<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Flow;
use App\Models\Order;
use App\Models\Rebate;
use App\Models\Withdraw;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;

//财务流水相关
class FlowController extends Controller
{
    //财务首页
    function index() {
        return view('admin.flow.index');
    }

    //财务数据
    function data(Request $request) {
        $model = Flow::query();
        if ($mobile = $request->get('mobile')) {
            $model->where('mobile',$mobile);
        }
        if ($start_time = $request->get('start_time')) {
            $model->where('flows.created_at','>=',$start_time);
        }
        if ($end_time = $request->get('end_time')) {
            $model->where('flows.created_at','<=',$end_time);
        }
        if ($type = $request->get('type')) {
            if ($type == 1) {//入账
                $model->where('type',ORDER);
            }elseif ($type == 2) {//出账
                $model->where('type',WITHDRAW);
            }
        }
        $res = $model->join('users','flows.user_id','=','users.id')->select('users.mobile','users.openid','flows.*')
                     ->orderBy('flows.id','asc')->paginate($request->get('limit',30));

        foreach ($res->items() as &$item) {
            switch ($item->type) {
                case ORDER:
                    $order = Order::find($item->business_id);
                    if ($order) {
                        switch ($order->status) {
                            case NO_PAY:
                                $item->status = '未支付';
                                break;
                            case TO_DELIVERY:
                                $item->status = '已支付';
                                break;
                            case USED:
                                $item->status = '已使用';
                                break;
                                //todo  过期的判断
                            case DELIVERY:
                                $item->status = '已发货';
                            default:
                                $item->status = '未知';
                                break;
                        }
                    }else {
                        $item->status = '不存在';
                    }

                    break;
                case REBATE:
                    $item->status = '已反润';
                    break;
                case WITHDRAW:
                    $withdraw = Withdraw::find($item->business_id);
                    if ($withdraw) {
                        switch ($withdraw->status) {
                            case SUCCESS_WITHDRAW:
                                $item->status = '成功';
                                break;
                            case FAIL_WITHDRAW:
                                $item->status = '失败';
                                break;
                            case PROCESSING_WITHDRAW:
                                $item->status = '处理中';
                                break;
                            default:
                                $item->status = '未知';
                                break;
                        }
                    }else {
                        $item->status = '不存在';
                    }
                    break;
                default:
                    $item->status = '未知';
                    break;
            }
        }
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res->total(),
            'data'  => $res->items(),
        ];

        return Response::json($data);
    }


}
